<template>
  <div class="announcements-page">
    <!-- 顶部横幅 -->
    <div class="banner">
      <div class="banner-content">
        <h1>通知公告</h1>
        <p>了解最新的活动信息与重要通知</p>
      </div>
    </div>

    <!-- 筛选区域 -->
    <div class="filter-section">
      <div class="filter-container">
        <el-radio-group v-model="activeType" size="large">
          <el-radio-button label="all">全部</el-radio-button>
          <el-radio-button label="activity">活动通知</el-radio-button>
          <el-radio-button label="notice">重要公告</el-radio-button>
          <el-radio-button label="recruit">招募信息</el-radio-button>
        </el-radio-group>

        <el-input
          v-model="searchKeyword"
          placeholder="搜索通知..."
          class="search-input"
          clearable>
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
      </div>
    </div>

    <!-- 公告列表 -->
    <div class="announcements-container">
      <div class="announcements-list">
        <div v-for="item in filteredAnnouncements" 
             :key="item.id" 
             class="announcement-item"
             @click="viewDetail(item.id)">
          <div class="item-content">
            <div class="item-title">
              <el-tag 
                :type="item.type" 
                size="small" 
                effect="light"
                class="item-tag"
              >
                {{ item.tagText }}
              </el-tag>
              <span class="text">{{ item.title }}</span>
            </div>
            <span class="time">{{ item.time }}</span>
          </div>
          <div class="item-desc">{{ item.description }}</div>
          <div class="item-meta">
            <span class="views">
              <el-icon><View /></el-icon>
              {{ item.views || 0 }} 次浏览
            </span>
            <span class="comments" v-if="item.comments">
              <el-icon><ChatLineRound /></el-icon>
              {{ item.comments }} 条评论
            </span>
          </div>
        </div>
      </div>

      <!-- 分页器 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { Search, View, ChatLineRound } from '@element-plus/icons-vue'

const router = useRouter()
const activeType = ref('all')
const searchKeyword = ref('')
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 示例数据
const announcements = ref([
  {
    id: 1,
    title: '关于组织海滩清洁志愿活动的通知',
    description: '为了保护海洋环境，我们将于本周末组织海滩清洁活动，欢迎广大志愿者参与...',
    time: '2024-03-15',
    type: 'success',
    tagText: '活动',
    views: 156,
    comments: 8
  },
  {
    id: 2,
    title: '2024年度海洋生态保护计划发布',
    description: '我们很高兴地宣布2024年度海洋生态保护计划正式发布，本计划包括...',
    time: '2024-03-10',
    type: 'primary',
    tagText: '公告',
    views: 328,
    comments: 12
  },
  {
    id: 3,
    title: '珊瑚礁保护项目招募志愿者',
    description: '珊瑚礁保护项目现正招募专业志愿者，具有海洋生物相关背景者优先...',
    time: '2024-03-05',
    type: 'warning',
    tagText: '招募',
    views: 245,
    comments: 15
  },
  {
    id: 4,
    title: '关于举办海洋环保知识讲座的通知',
    description: '为提高公众海洋环保意识，定于本月15日举办海洋环保知识讲座...',
    time: '2024-03-01',
    type: 'info',
    tagText: '讲座',
    views: 189
  }
])

// 根据筛选条件过滤公告
const filteredAnnouncements = computed(() => {
  let filtered = announcements.value
  
  // 根据类型筛选
  if (activeType.value !== 'all') {
    filtered = filtered.filter(item => {
      const typeMap = {
        'activity': ['活动', '讲座'],
        'notice': ['公告'],
        'recruit': ['招募']
      }
      return typeMap[activeType.value]?.includes(item.tagText)
    })
  }
  
  // 根据关键词搜索
  if (searchKeyword.value) {
    const keyword = searchKeyword.value.toLowerCase()
    filtered = filtered.filter(item => 
      item.title.toLowerCase().includes(keyword) || 
      item.description.toLowerCase().includes(keyword)
    )
  }
  
  return filtered
})

// 查看详情
const viewDetail = (id) => {
  router.push(`/announcements/${id}`)
}

const handleSizeChange = (val) => {
  pageSize.value = val
  currentPage.value = 1
}

const handleCurrentChange = (val) => {
  currentPage.value = val
}
</script>

<style scoped lang="scss">
.announcements-page {
  min-height: 100vh;
  background-color: #f5f7fa;

  .banner {
    background: linear-gradient(rgba(0,0,0,0.6), rgba(0,0,0,0.6)),
                url('https://images.unsplash.com/photo-1439405326854-014607f694d7?w=1920') center/cover;
    color: white;
    padding: 60px 0;
    margin-bottom: 40px;
    border-radius: 8px;
    text-align: center;

    .banner-content {
      h1 {
        font-size: 2.5em;
        margin-bottom: 15px;
      }
      p {
        font-size: 1.2em;
        opacity: 0.9;
      }
    }
  }

  .filter-section {
    max-width: 1200px;
    margin: 0 auto 30px;
    padding: 0 20px;

    .filter-container {
      display: flex;
      justify-content: space-between;
      align-items: center;
      gap: 20px;

      .search-input {
        width: 300px;
      }
    }
  }

  .announcements-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;

    .announcements-list {
      .announcement-item {
        background: white;
        border-radius: 8px;
        padding: 20px;
        margin-bottom: 20px;
        cursor: pointer;
        transition: all 0.3s ease;
        box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);

        &:hover {
          transform: translateY(-2px);
          box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
        }

        .item-content {
          display: flex;
          justify-content: space-between;
          align-items: flex-start;
          margin-bottom: 12px;

          .item-title {
            display: flex;
            align-items: center;
            gap: 12px;
            flex: 1;
            min-width: 0;

            .item-tag {
              flex-shrink: 0;
            }

            .text {
              font-size: 16px;
              font-weight: 500;
              color: #1f2937;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
          }

          .time {
            color: #64748b;
            font-size: 14px;
            margin-left: 16px;
            flex-shrink: 0;
          }
        }

        .item-desc {
          color: #64748b;
          font-size: 14px;
          line-height: 1.6;
          margin-bottom: 16px;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }

        .item-meta {
          display: flex;
          gap: 20px;
          color: #94a3b8;
          font-size: 13px;

          span {
            display: flex;
            align-items: center;
            gap: 4px;
          }
        }
      }
    }

    .pagination {
      display: flex;
      justify-content: center;
      margin-top: 40px;
      margin-bottom: 40px;
    }
  }
}

@media (max-width: 768px) {
  .announcements-page {
    .filter-section {
      .filter-container {
        flex-direction: column;
        align-items: stretch;

        .search-input {
          width: 100%;
        }
      }
    }

    .announcements-container {
      .announcement-item {
        .item-content {
          flex-direction: column;
          gap: 8px;

          .time {
            margin-left: 0;
          }
        }
      }
    }
  }
}
</style> 